package middlewareorid.ui;

import middlewareorid.engine.Config;
import middlewareorid.engine.ConfigAccess;
import superwaba.ext.xplat.ui.MultiEdit;
import waba.fx.Color;
import waba.fx.Rect;
import waba.sys.Convert;
import waba.ui.Button;
import waba.ui.ComboBox;
import waba.ui.Container;
import waba.ui.ControlEvent;
import waba.ui.Edit;
import waba.ui.Event;
import waba.ui.Label;

public class PreferenceBlogWindow extends Container {

	private ComboBox cbApps;
	
	private ComboBox cbStyle;
	
	private ComboBox cbUI;
	
	private Edit edUser;
	
	private Edit edPassword;

	private MultiEdit edURL;
	
	private Button btOK;
	
	private ConfigAccess cf;

	public PreferenceBlogWindow(Rect rect) {

		setRect(rect);
		setBackColor(Color.WHITE);
	}

	public void onStart() {
		cf = new ConfigAccess();
		cf.init();
		init();
	}

	private void init() {
		// Caption
		Label info = new Label("  Configuration  ");
		
		// Apps Type
		info.setBackForeColors(Color.BLACK, Color.WHITE);
		add(info, LEFT, TOP);
		info = new Label("Blog/Weblog : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 5);
		cbApps = new ComboBox();
		cbApps.insert("Blogs.Or.Id", 0);
		cbApps.insert("WordPress", 1);
		add(cbApps, AFTER + 5, SAME);

		// URL Address
		info = new Label("Blog/Weblog Address : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 3);		
		edURL = new MultiEdit(2, 0);
		add(edURL);
		edURL.setRect(LEFT + 5, AFTER + 3, getClientRect().width - 10, PREFERRED);
		edURL.setDrawDots(false);
		
		// User Name
		info = new Label("User ID  : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 3);
		edUser = new Edit();
		add(edUser, getClientRect().width/3, SAME);

		// User Name
		info = new Label("Password : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 3);
		edPassword = new Edit();
		edPassword.setMode(Edit.PASSWORD_ALL);
		add(edPassword, getClientRect().width/3, SAME);
		
		// Style Name
		info = new Label("Style : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 3);
		cbStyle = new ComboBox();
		cbStyle.insert("Default", 0);
		cbStyle.insert("Dark", 1);
		cbStyle.insert("Funky", 2);
		add(cbStyle, getClientRect().width/3, SAME);		

		// Style Name
		info = new Label("UI : ");
		info.setBackColor(Color.WHITE);
		add(info, LEFT + 5, AFTER + 3);
		cbUI = new ComboBox();
		cbUI.insert("WinCE", 0);
		cbUI.insert("Flat", 1);
		cbUI.insert("PalmOS", 2);
		add(cbUI, getClientRect().width/3, SAME);		

		btOK = new Button(" Apply ");
		add(btOK, LEFT + 5, AFTER + 10);				

		String blogtype = cf.getConfigFromCatalog(cf.APP_BLOGS_TYPE);		
		cbApps.select(Convert.toInt(blogtype));		
		edURL.setText(cf.getConfigFromCatalog(cf.APP_BLOGS));
		edUser.setText(cf.getConfigFromCatalog(cf.APP_USER));
		edPassword.setText(cf.getConfigFromCatalog(cf.APP_PASSWORD));
		cbStyle.select(Convert.toInt(cf.getConfigFromCatalog(cf.APP_STYLE)));	
		cbUI.select(Convert.toInt(cf.getConfigFromCatalog(cf.APP_UI)));
	}
	
	public void onEvent(Event event) {
		switch (event.type) {
		case ControlEvent.PRESSED:
			if (event.target == btOK) {
				cf.setConfigToCatalog(cf.APP_BLOGS, edURL.getText());
				cf.setConfigToCatalog(cf.APP_USER, edUser.getText());
				cf.setConfigToCatalog(cf.APP_PASSWORD, edPassword.getText());
				cf.setConfigToCatalog(cf.APP_STYLE, Convert.toString(cbStyle.getSelectedIndex()));
				cf.setConfigToCatalog(cf.APP_UI, Convert.toString(cbUI.getSelectedIndex()));
				
				ConfigAccess cf = new ConfigAccess();
				int style = Convert.toInt(cf.getConfigFromCatalog(cf.APP_STYLE));				
				setStyle(style);
			}
			break;
		}
	}

	public void setStyle(int style) {
		setBackColor(Config.background[style]);
	}	
}
